package com.impetus.project.skilltest.client.pages;

import com.impetus.project.skilltest.common.SkillTestException;
import com.impetus.project.skilltest.common.dataobject.Candidate;

public class PageRouter {
	/** The message displayed to the user when the server cannot be reached or
	 *  returns an error.
	 */
	@SuppressWarnings("unused")
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";
	
	public static void loadPage(String pageName, PageData pageData)
	{
		System.out.println("PageRouter.loadPage(): pageName["+pageName+"], PageData["+pageData+"]");
		
		if(pageName == null || pageName.equals("") || pageName.equals(GUIConstants.PAGES.LOGIN_PAGE)){
			// Load the login page
			new LoginPage().loadPage(pageData);
			pageData.setLastPageLoaded(GUIConstants.PAGES.LOGIN_PAGE);
		}
		else{
			if(checkLogin(pageData.getUser()))
			{
				try
				{
					pageData.setLastPageLoaded(pageName);
					pageData.setExceptionCaught(null);
					
					loadPageImpl(pageName, pageData);					
				}
				catch(Throwable caught)
				{
					pageData.setExceptionCaught(caught);
					new ErrorPage().loadPage(pageData);
					// TODO: log error in the system
				}
			}
			else
			{
				loadPage(GUIConstants.PAGES.LOGIN_PAGE, pageData);
			}
		}
	}
	
	private static void loadPageImpl(String pageName, PageData pageData)throws SkillTestException
	{
		try
		{
			if(GUIConstants.PAGES.LOGIN_PAGE.equalsIgnoreCase(pageName)){
				new LoginPage().loadPage(pageData);
			}
			else if(GUIConstants.PAGES.TEST_LISTING_PAGE.equalsIgnoreCase(pageName)){
				new TestListingPage().loadPage(pageData);
			}	
			else if(GUIConstants.PAGES.TEST_PARTICIPATION_PAGE.equalsIgnoreCase(pageName)){
				new TestParticipationPage().loadPage(pageData);
			}
			else if(GUIConstants.PAGES.TEST_SUMMARY_PAGE.equalsIgnoreCase(pageName)){
				new TestListingPage().loadPage(pageData);
			}
			else if(GUIConstants.PAGES.TEST_EDIT_PAGE.equalsIgnoreCase(pageName)){
				new TestEditPage().loadPage(pageData);
			}
			else if(GUIConstants.PAGES.CANDIDATE_DETAILS_PAGE.equalsIgnoreCase(pageName)){
				// TODO: cerate new page
			}
			else if(GUIConstants.PAGES.CANDIDATE_LIST_PAGE.equalsIgnoreCase(pageName)){
				// TODO: cerate new page
			}
			else if(GUIConstants.PAGES.QUESTION_DETAILS_PAGE.equalsIgnoreCase(pageName)){
				// TODO: cerate new page
			}
			else if(GUIConstants.PAGES.QUESTION_LIST_PAGE.equalsIgnoreCase(pageName)){
				// TODO: cerate new page
			}
			else if(GUIConstants.PAGES.ERROR_PAGE.equalsIgnoreCase(pageName)){
				new ErrorPage().loadPage(pageData);
			}
			
		}
		catch(Throwable t)
		{
			if(t instanceof SkillTestException){
				throw (SkillTestException)t;
			}else{
				throw new SkillTestException(t);
			}
		}	
	}
	
	public static boolean checkLogin(Candidate candidate){
		boolean ret = true;
		
		return ret;
	}
}
